package com.ourteam.product.ui.dao.impl;

import com.ourteam.product.ui.dao.IProductUITreeNodeDAO;
import com.ourteam.product.ui.dao.ProductUITreeNode;
import com.ourteam.product.ui.dao.ProductUITreeNodeDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-12-14 15:15:19 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractProductUITreeNodeDAOJdbcImpl extends AbstractSpringDAO
    implements IProductUITreeNodeDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        TreeId,
        Code,
        Title,
        TitleKey,
        Icon,
        IsLeaf,
        LastModifyUser,
        LastModifyTime,
        Status,
        Remarks,
        IsRoot
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.TreeId,
        _DataObjectProperty.Code,
        _DataObjectProperty.Title,
        _DataObjectProperty.TitleKey,
        _DataObjectProperty.Icon,
        _DataObjectProperty.IsLeaf,
        _DataObjectProperty.LastModifyUser,
        _DataObjectProperty.LastModifyTime,
        _DataObjectProperty.Status,
        _DataObjectProperty.Remarks,
        _DataObjectProperty.IsRoot
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<ProductUITreeNode, ProductUITreeNodeDAOQueryBean> daoHelper;

  public AbstractProductUITreeNodeDAOJdbcImpl() {
    super();
    daoHelper = new DataAccessObjectHelper<ProductUITreeNode, ProductUITreeNodeDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aProductUITreeNode
   * @throws Exception
   */
  public boolean checkIsUnique(ProductUITreeNode aProductUITreeNode) throws Exception {

    if (aProductUITreeNode == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aProductUITreeNode.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aProductUITreeNode
   * @throws Exception
   */
  public void insert(ProductUITreeNode aProductUITreeNode) throws Exception {

    PropertyUtils.setProperty(
        aProductUITreeNode,
        _DataObjectProperty.LastModifyTime,
        new java.sql.Timestamp(System.currentTimeMillis()));

    super.doInsert(aProductUITreeNode);
  }

  /**
   * Batch Insert Datas
   *
   * @param aProductUITreeNode
   * @throws Exception
   */
  public void batchInsert(ProductUITreeNode[] aProductUITreeNodes) throws Exception {

    super.doBatchInsert(aProductUITreeNodes);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(ProductUITreeNodeDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(ProductUITreeNode aProductUITreeNode) throws Exception {

    delete(aProductUITreeNode.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aProductUITreeNode
   * @throws Exception
   */
  public void update(ProductUITreeNode aProductUITreeNode) throws Exception {

    PropertyUtils.setProperty(
        aProductUITreeNode,
        _DataObjectProperty.LastModifyTime,
        new java.sql.Timestamp(System.currentTimeMillis()));

    if (aProductUITreeNode.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aProductUITreeNode);

    ProductUITreeNode tempObj = findById(aProductUITreeNode.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aProductUITreeNode, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aProductUITreeNodes[]
   * @throws Exception
   */
  public void batchUpdate(ProductUITreeNode[] aProductUITreeNodes) throws Exception {

    super.doBatchUpdate(aProductUITreeNodes);
  }

  /**
   * Update Data By Condictions
   *
   * @param aProductUITreeNode
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(ProductUITreeNode aProductUITreeNode, ProductUITreeNodeDAOQueryBean aQueryBean)
      throws Exception {

    PropertyUtils.setProperty(
        aProductUITreeNode,
        _DataObjectProperty.LastModifyTime,
        new java.sql.Timestamp(System.currentTimeMillis()));

    super.doUpdateByQuery(aProductUITreeNode, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(ProductUITreeNodeDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(ProductUITreeNode aProductUITreeNode) throws Exception {

    if (aProductUITreeNode.getCode() == null) {
      throw new Exception("The ProductUITreeNode.code value is not allow NULL");
    }

    if (aProductUITreeNode.getTitle() == null) {
      throw new Exception("The ProductUITreeNode.title value is not allow NULL");
    }

    if (aProductUITreeNode.getTitleKey() == null) {
      throw new Exception("The ProductUITreeNode.titleKey value is not allow NULL");
    }

    if (aProductUITreeNode.getIcon() == null) {
      throw new Exception("The ProductUITreeNode.icon value is not allow NULL");
    }

    if (aProductUITreeNode.getIsLeaf() == null) {
      throw new Exception("The ProductUITreeNode.isLeaf value is not allow NULL");
    }

    if (aProductUITreeNode.getLastModifyUser() == null) {
      throw new Exception("The ProductUITreeNode.lastModifyUser value is not allow NULL");
    }

    if (aProductUITreeNode.getLastModifyTime() == null) {
      throw new Exception("The ProductUITreeNode.lastModifyTime value is not allow NULL");
    }

    if (aProductUITreeNode.getStatus() == null) {
      throw new Exception("The ProductUITreeNode.status value is not allow NULL");
    }

    if (aProductUITreeNode.getRemarks() == null) {
      throw new Exception("The ProductUITreeNode.remarks value is not allow NULL");
    }
  }

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public ProductUITreeNode findById(final long id) throws Exception {

    ProductUITreeNodeDAOQueryBean queryBean = new ProductUITreeNodeDAOQueryBean();

    ProductUITreeNodeDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    ProductUITreeNode[] ProductUITreeNodes = query(queryBean);

    if (ProductUITreeNodes.length == 0) {
      return null;
    } else {
      return ProductUITreeNodes[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return ProductUITreeNode[]
   * @throws Exception
   */
  public ProductUITreeNode[] query(ProductUITreeNodeDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcProductUITreeNodeRowMapper());

    return (ProductUITreeNode[]) result.toArray(new ProductUITreeNode[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(ProductUITreeNodeDAOQueryBean queryBean)
      throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(ProductUITreeNodeDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      ProductUITreeNode[] aProductUITreeNodes, ProductUITreeNodeDAOQueryBean queryBean)
      throws Exception {
    daoHelper.synchronize(aProductUITreeNodes, queryBean);
  }

  public void synchronize(
      ProductUITreeNode[] aProductUITreeNodes,
      ProductUITreeNodeDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<ProductUITreeNode> callBack)
      throws Exception {
    daoHelper.synchronize(aProductUITreeNodes, queryBean, callBack);
  }

  public static class JdbcProductUITreeNodeRowMapper implements RowMapper<ProductUITreeNode> {

    public ProductUITreeNode mapRow(ResultSet resultSet, int arg1) throws SQLException {
      ProductUITreeNode object = new ProductUITreeNode();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object treeId = null;

        treeId = resultSet.getObject(columnIndex++);

        object.setTreeIdNull(resultSet.getBoolean(columnIndex++));

        if (treeId != null) {

          setBeanValue(object, _DataObjectProperty.TreeId, treeId);
        }

        Object code = null;

        code = resultSet.getObject(columnIndex++);

        object.setCodeNull(resultSet.getBoolean(columnIndex++));

        if (code != null) {

          setBeanValue(object, _DataObjectProperty.Code, code);
        }

        Object title = null;

        title = resultSet.getObject(columnIndex++);

        object.setTitleNull(resultSet.getBoolean(columnIndex++));

        if (title != null) {

          setBeanValue(object, _DataObjectProperty.Title, title);
        }

        Object titleKey = null;

        titleKey = resultSet.getObject(columnIndex++);

        object.setTitleKeyNull(resultSet.getBoolean(columnIndex++));

        if (titleKey != null) {

          setBeanValue(object, _DataObjectProperty.TitleKey, titleKey);
        }

        Object icon = null;

        icon = resultSet.getObject(columnIndex++);

        object.setIconNull(resultSet.getBoolean(columnIndex++));

        if (icon != null) {

          setBeanValue(object, _DataObjectProperty.Icon, icon);
        }

        Object isLeaf = null;

        isLeaf = resultSet.getObject(columnIndex++);

        object.setIsLeafNull(resultSet.getBoolean(columnIndex++));

        if (isLeaf != null) {

          setBeanValue(object, _DataObjectProperty.IsLeaf, isLeaf);
        }

        Object lastModifyUser = null;

        lastModifyUser = resultSet.getObject(columnIndex++);

        object.setLastModifyUserNull(resultSet.getBoolean(columnIndex++));

        if (lastModifyUser != null) {

          setBeanValue(object, _DataObjectProperty.LastModifyUser, lastModifyUser);
        }

        Object lastModifyTime = null;

        __dataType = resultSet.getMetaData().getColumnType(columnIndex);

        if (__dataType == java.sql.Types.DATE
            || __dataType == java.sql.Types.TIME
            || __dataType == java.sql.Types.TIMESTAMP) {
          lastModifyTime = resultSet.getTimestamp(columnIndex++);

        } else {
          lastModifyTime = resultSet.getObject(columnIndex++);
        }

        object.setLastModifyTimeNull(resultSet.getBoolean(columnIndex++));

        if (lastModifyTime != null) {

          setBeanValue(object, _DataObjectProperty.LastModifyTime, lastModifyTime);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object remarks = null;

        remarks = resultSet.getObject(columnIndex++);

        object.setRemarksNull(resultSet.getBoolean(columnIndex++));

        if (remarks != null) {

          setBeanValue(object, _DataObjectProperty.Remarks, remarks);
        }

        Object isRoot = null;

        isRoot = resultSet.getObject(columnIndex++);

        object.setIsRootNull(resultSet.getBoolean(columnIndex++));

        if (isRoot != null) {

          setBeanValue(object, _DataObjectProperty.IsRoot, isRoot);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
